package com.edgclub.edg.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.edgclub.edg.R;
import com.edgclub.edg.activity.GzTopicDetailActivity;
import com.edgclub.edg.activity.UserInfoOtherActivity;
import com.edgclub.edg.adapter.adapteruitls.GzBaseAdapter;
import com.edgclub.edg.adapter.adapteruitls.MyItemClickListener;
import com.edgclub.edg.adapter.adapteruitls.MyItemLongClickListener;
import com.edgclub.edg.adapter.adapteruitls.RViewHolder;
import com.edgclub.edg.model.RecommendTopic;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;
import com.edgclub.edg.utils.util.DensityUtils;
import com.rockerhieu.emojicon.EmojiconTextView1;

import java.util.ArrayList;


/**
 */
public class FindIndexAdapter extends GzBaseAdapter{
    private ArrayList<RecommendTopic> detailDatas = null;
    private Context mContext;
    private GlideUtils glideUtils;
    private int width;

    private LikeClickListener likeClickListener;

    public FindIndexAdapter(Context context) {
        this.mContext = context;
        glideUtils = new GlideUtils();
        width = (DensityUtils.getScreenW((Activity) mContext)-(int) mContext.getResources().getDimension(
                R.dimen.size_30))/2;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public FindIndexAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_find_index,viewGroup,false);
            return new ViewHolder(view,mListener,mLongClickListener);
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder1, final int position) {
        FindIndexAdapter.ViewHolder viewHolder= (ViewHolder) viewHolder1;
            final RecommendTopic topic = detailDatas.get(position);
            if(topic.getPosts()!=null && topic.getPosts().getUser()!=null){
                viewHolder.user_name.setText(topic.getPosts().getUser().getNick()+"");
                glideUtils.Glidde_loadCropCircleImage(mContext,topic.getPosts().getUser().getLogo(),viewHolder.user_icon);
                viewHolder.user_icon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent=new Intent(mContext, UserInfoOtherActivity.class);
                        intent.putExtra("user",topic.getPosts().getUser());
                        mContext.startActivity(intent);
                    }
                });
                if(null!=topic.getPosts().getUser().getUserAuth()){
                    if ("-1".equals(topic.getPosts().getUser().getUserAuth())){
                        viewHolder.vip.setVisibility(View.GONE);
                    }
                    if("0".equals(topic.getPosts().getUser().getUserAuth().getType())){
                        viewHolder.vip.setVisibility(View.VISIBLE);
                        viewHolder.vip.setImageResource(R.drawable.v_huan);
                    }else if("1".equals(topic.getPosts().getUser().getUserAuth().getType())){
                        viewHolder.vip.setVisibility(View.VISIBLE);
                        viewHolder.vip.setImageResource(R.drawable.v_lan);
                    }
                }else{
                    viewHolder.vip.setVisibility(View.GONE);
                }


            }

            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) viewHolder.ll_pan.getLayoutParams();
            layoutParams.width = width;
           viewHolder.ll_pan.setLayoutParams(layoutParams);

            ViewGroup.LayoutParams params = viewHolder.imgv_cover.getLayoutParams();
            params.width = width;
            try{
                if(topic.getWidth()==0 || topic.getHeight()==0){
                    if(TextUtils.isEmpty(topic.getCover()) && topic.getPosts().getAlbums() != null && topic.getPosts().getAlbums().size() > 0){
                        params.height = width*topic.getPosts().getAlbums().get(0).getHeight()/topic.getPosts().getAlbums().get(0).getWidth();
                    }else{
                        params.height = width;
                    }
                }else{
                    params.height = width*topic.getHeight()/topic.getWidth();
                }
                viewHolder.imgv_cover.setLayoutParams(params);
            } catch (Exception e){
                e.printStackTrace();
            }

            if (!TextUtils.isEmpty(topic.getCover())) {
                glideUtils.Glidde_loadRoudImage2(mContext, topic.getCover(), viewHolder.imgv_cover, 12);
            } else {
                if (topic.getPosts().getAlbums() != null && topic.getPosts().getAlbums().size() > 0) {

                    glideUtils.Glidde_loadRoudImage2(mContext, topic.getPosts().getAlbums().get(0).getThumbnail(), viewHolder.imgv_cover, 12);
                }
            }
            /*****************内容*****************/
            viewHolder.topic_content.setVisibility(View.VISIBLE);
            if(TextUtils.isEmpty(topic.getTitle())){
//                viewHolder.topic_content.setVisibility(View.GONE);
                viewHolder.topic_content.setText(topic.getPosts().getContent()+"");
            }else{
                viewHolder.topic_content.setText(topic.getTitle());
            }

            viewHolder.ll_zan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(likeClickListener!=null){
                        likeClickListener.like(position);
                    }
                }
            });
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    jumpTopicDetailActivity(position);
                }
            });

        if(topic.getPosts()!=null){
            if(Integer.parseInt(topic.getPosts().getReplys())>0){
                viewHolder.tv_comment_count.setText(topic.getPosts().getReplys());
            }else{
                viewHolder.tv_comment_count.setText("0");
            }
            if(Integer.parseInt(topic.getPosts().getLikes())>0){
                viewHolder.tv_zan_count.setText(topic.getPosts().getLikes());
            }else{
                viewHolder.tv_zan_count.setText("0");
            }
        }

    }


    //获取数据的数量
    @Override
    public int getItemCount() {
        if(detailDatas==null)return 0;
        return detailDatas.size();
    }


    public ArrayList<RecommendTopic> getDetailDatas() {
        return detailDatas;
    }

    public void setDetailDatas(ArrayList<RecommendTopic> detailDatas) {
        this.detailDatas = detailDatas;
    }

    public interface LikeClickListener {
        void like(int position);
    }

    public LikeClickListener getLikeClickListener() {
        return likeClickListener;
    }

    public void setLikeClickListener(LikeClickListener likeClickListener) {
        this.likeClickListener = likeClickListener;
    }

    public void jumpTopicDetailActivity(int position){
        Intent intent = new Intent(mContext, GzTopicDetailActivity.class);
        if(detailDatas.get(position).getPosts()!=null){
            intent.putExtra("topicId", detailDatas.get(position).getPosts().getPostsId());
        }
        mContext.startActivity(intent);
        ((Activity)mContext).overridePendingTransition(R.anim.slide_in_from_right,
                R.anim.slide_out_to_left);
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RViewHolder {
        private EmojiconTextView1 topic_content;
        private TextView topic_time,user_name;
        private ImageView user_icon,imgv_cover,vip;
        private TextView tv_comment_count,tv_zan_count;

        //
        LinearLayout ll_zan,ll_comment,ll_pan;

        public ViewHolder(View view, MyItemClickListener mListener, MyItemLongClickListener mLongClickListener){
            super(view,mListener,mLongClickListener);
            topic_content = (EmojiconTextView1) view.findViewById(R.id.topic_content);
            topic_time = (TextView) view.findViewById(R.id.topic_time);
            user_name = (TextView) view.findViewById(R.id.user_name);
            user_icon = (ImageView) view.findViewById(R.id.user_icon);
            imgv_cover = (ImageView) view.findViewById(R.id.imgv_cover);
            vip = (ImageView) view.findViewById(R.id.vip);
            ll_zan = (LinearLayout) view.findViewById(R.id.ll_zan);
            ll_comment = (LinearLayout) view.findViewById(R.id.ll_comment);
            ll_pan = (LinearLayout) view.findViewById(R.id.ll_pan);
            tv_zan_count = (TextView) view.findViewById(R.id.tv_like_num);
            tv_comment_count = (TextView) view.findViewById(R.id.tv_comment_count);

        }
    }




}
